// tx/data/data.js
let App = getApp();
var that;
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		list: ['借记卡', '信用卡'],
		index: 0,
		bank_username: '',
		id_card: '',
		bank_name: '',
    card_type: '借记卡',
		card_no: '',
		re_card_no:'',
		datacard: ''
	},
	//开户用户名称
	bank_username(e){
	  this.setData({
	    bank_username: e.detail.value
	  })
	},
//开户用户名称
	bank_name(e){
	  this.setData({
	    bank_name: e.detail.value
	  })
	},
	//身份证号码
	id_card(e){
	  this.setData({
	    id_card: e.detail.value
	  })
	},
	//开户用户
	bank_username(e){
	  this.setData({
	    bank_username: e.detail.value
	  })
	},
	//银行卡卡号
	card_no(e){
	  this.setData({
	    card_no: e.detail.value
	  })
	},
	re_card_no(e){
	  this.setData({
	    re_card_no: e.detail.value
	  })
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	userBankInfo() {
		that = this;
		wx.request({
			url: App.url.requestUrl + 'user/userBankInfo',
			method: 'POST',
			 header: {'content-type': 'application/x-www-form-urlencoded',"token":wx.getStorageSync('access_token')},
			dataType: 'json',
			success: function(res) {
				if (res.data.ret == 200) {
          console.log(res.data.data)
          if (res.data.data.card_type =='信用卡'){
            that.setData({
              index:1
            })
          }else{
            that.setData({
              index: 0
            })
          }
					that.setData({
						datacard: res.data.data,
            bank_username: res.data.data.bank_username,
            id_card: res.data.data.id_card,
            bank_name: res.data.data.bank_name,
            
            card_no: res.data.data.card_no,
            re_card_no: res.data.data.re_card_no
           
            
					})
				} else {
					wx.showToast({
						title: res.data.msg,
						icon: 'none',
						duration: 2000
					})
				}
			}
		})
	},
	bindpickerchange(e) {
		this.setData({
			index: e.detail.value,
			card_type: that.data.list[e.detail.value]
		})
		console.log( that.data.list[e.detail.value])
	},
	//提交申请
	applyPut() {
		that = this;
		if (that.data.bank_username == '') {
		    wx.showModal({
		        title: '请填写开户人姓名',
		        showCancel: false
		    });
		    return;
		}
		if (that.data.id_card == '') {
		    wx.showModal({
		        title: '请填写开户人身份证号码',
		        showCancel: false
		    });
		    return;
		}
		if (that.data.bank_name == '') {
		    wx.showModal({
		        title: '请填写开户银行',
		        showCancel: false
		    });
		    return;
		}
		if (that.data.card_no == '') {
		    wx.showModal({
		        title: '请填写银行卡卡号',
		        showCancel: false
		    });
		    return;
		}
		
		wx.request({
			url: App.url.requestUrl + 'user/applyPut',
			method: 'POST',
			 header: {'content-type': 'application/x-www-form-urlencoded',"token":wx.getStorageSync('access_token')},
			dataType: 'json',
			data: {
				bank_username: that.data.bank_username,
				id_card: that.data.id_card,
				bank_name: that.data.bank_name,
				card_no: that.data.card_no,
				card_type: that.data.card_type,
        re_card_no: that.data.re_card_no
			},
			success: function(res) {
				if (res.data.ret == 200) {
					wx.showToast({
						title: res.data.msg,
						icon: 'success',
						duration: 2000
					})
          setTimeout(function(){
            wx.navigateBack({
              delta: 1,
            })
          },2000)
         
				} else {
					wx.showToast({
						title: res.data.msg,
						icon: 'none',
						duration: 2000
					})
				}
			}
		})
	},
	onLoad: function(options) {
		that = this;
		that.userBankInfo()
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	}
})
 